package net.longjin.config;

import net.longjin.annotation.LoginUser;
import net.longjin.controller.LoginController;
import net.longjin.entity.UserInfo;
import net.longjin.utils.DICTokenUtil;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class LoginAccountHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return (methodParameter.getParameterType().isAssignableFrom(UserInfo.class) &&
                methodParameter.hasParameterAnnotation(LoginUser.class));
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
       Object object = nativeWebRequest.getAttribute("userInfo", RequestAttributes.SCOPE_REQUEST);
        UserInfo userInfo = null;
        if (object != null) {
            userInfo = (UserInfo) object;
        }
        return userInfo;

    }
}
